package com.example.wzsdk.login;

import com.example.wzsdk.commpg.SetPackages;
import com.example.wzsdk.constant.Data;

import java.util.Arrays;

/**
 * Created by SunLight on 2017/8/14.
 */

public class SendEven {

    LoginFinshListing loginFinshListing;
    HeartThread heartThread;
    LoginThread loginThread;

    public interface LoginFinshListing {
        void LoginFinsh(boolean state);
    }

    public SendEven(LoginFinshListing loginFinshListing, LoginThread loginThread) {
        this.loginFinshListing = loginFinshListing;
        this.loginThread = loginThread;
    }

    public void HeartClose() {
        heartThread.isRun = false;
        heartThread.interrupt();
    }

    public void SendDataEven(SetPackages setPackages, byte[] number, Data data) {
        switch (number[6]) {
            case 8:
                if (number[5] == 1) {
                    loginThread.Send(setPackages.LoginPg());
                } else {
                    loginThread.Send(setPackages.BulkPg());
                }
                break;
            case 2:
                switch (number[5]) {
                    case 3:
                        loginThread.Send(setPackages.BulkPg(4));
                        break;
                    case 4:
                        loginThread.Send(setPackages.BulkPg(1));
                        break;
                    case 5:
                        if (data.Length[0] > 70) {
                            loginThread.Send(setPackages.BulkPg(2));
                        } else {
                            Corresponding(setPackages, data);
                        }
                        break;
                }
                break;
        }
    }

    public void Corresponding(SetPackages setPackages, Data data) {
        heartThread = new HeartThread(setPackages, data,loginThread);
        for (int i = 0; i < data.Name.size(); i++) {
            for (int j = 0; j < data.Id_Str.size(); j++) {
                if (data.Name.get(i).equals(data.Id_Str.get(j))) {
                    data.I.add(i);
                    data.Node.add(new String[]{setPackages.Zhuan(new byte[]{data.Sid.get(i)[0], data.Sid.get(i)[1]}, 10), setPackages.Zhuan(data.FileID.get(j), 10), data.VarIdx.get(i) + "", i + "", data.State.get(j) + "", j + ""});
                }
            }
        }
        for (int j = 0; j < data.Node.size(); j++) {
            if (data.Node.get(j)[2].equals("1")) {
                data.J.add(Integer.parseInt(data.Node.get(j)[3]));
            } else if (data.Node.get(j)[2].equals("2") && data.Node.get(j)[1].equals("30008")) {
                data.J.add(Integer.parseInt(data.Node.get(j)[3]));
            }
        }
        if (data.J.size() != 0) {
            loginFinshListing.LoginFinsh(true);
            heartThread.start();
        } else {
            loginFinshListing.LoginFinsh(false);
        }
    }
}
